app.register.controller('CarstockCtrl', function($scope, $http, $rootScope,
		$route, CarstockSer, AppUtil, $q,$routeParams) {
	
	loadFlag($routeParams.id,$scope);
	//获取本地存储中的页码页数
	var page = AppUtil.GetLocalValue("carstockTable_Page");
	var size = AppUtil.GetLocalValue("carstockTable_Size");
	$scope.pagination = {
			totalItems :0,
			currentPage : page == undefined ? 1 : page,
			itemsPerPage : size == undefined ? 10 : size
	};
	
	$scope.SearchData = function() {
		$scope.postData={};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		// 表单查询参数
		LoadList();
	};


    $scope.getStorage = function ($event) {
        //alert($scope.entityCode);
        //alert( $($event.target).attr("val_code"));
        // 加载下拉框仓库数据
        $http.post("carStock/loadStorage",{"entity_code":$($event.target).attr("val_code")}).success(function(datas) {
            $scope.storage_list = datas.data.storage_list;
        });
    }
	
	// 加载数据方法
	var LoadList = function() {
		if ($scope.postData == undefined)
			$scope.postData = {};
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, $scope.selected,params);
		// 当有查询参数时,重置页数
		if (AppUtil.Length(params) > 0) {
			$scope.postData.params = params;
		}
		// 筛选查询参数
		var search_opt = AppUtil.GetSearchOpt();
		if (search_opt.length > 0) {
			$scope.postData.search_opt = search_opt;
		}
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		// 加载数据
		CarstockSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.pagination.totalItems = response.total;
			$scope.carstock = response.data.data
            $scope.stockSum = response.data.sum;
			setTimeout(check_table_tr,500);
		});
	}
	// 配置分页基本参数
	$scope.$watch('pagination.refresh',LoadList);

    $scope.$watch('entityInfo',function () {
        $http.post('carStock/loadStorage', {"entityInfo":$scope.entityInfo}).success(function (data) {
            $scope.storage_list = data.data.storage_list;
        });
	});
	
	//加载下拉列表数据
	$http.get("carStock/dicts").success(function(data){
		$scope.stockStatus_list=data.data.stockStatus_list;
		$scope.masslossStatus_list=data.data.masslossStatus_list;
		//$scope.storage_list=data.data.storage_list;
		$scope.vehicleStatus_list=data.data.vehicleStatus_list;
	});

    // 加载品牌
    $http.get("common/brands").success(function(data) {
        $scope.brands = data;
    });
    // 加载下拉框仓库数据
	$http.post("carStock/loadStorage",{}).success(function(datas) {
		$scope.storage_list = datas.data.storage_list;
	});
	// 加载门店
	AppUtil.LoadGroups();
	
	// 导出
	$scope.ExportData = function() {
		AppUtil.ExportData($scope.postData);
	};
});

app.register.service('CarstockSer', function($http,AppUtil) {
	this.list = function(postData) {
		AppUtil.loading();
		//设置查询开始下标
		postData.start= (postData.page-1<0?0:postData.page-1)*postData.size;
		return $http.post('carStock/list', postData).error(function() {
			AppUtil.remove_loading();
		});;
	};

});

